//
//  ViewController.m
//  高仿酷狗音乐歌词逐渐字播放
//
//  Created by liuyang on 16/3/21.
//  Copyright © 2016年 ly. All rights reserved.
//

#import "ViewController.h"
#import "LyricsUtil.h"
#import "LyricsView.h"
#import "KRC.h"
#import <AVFoundation/AVFoundation.h>
#import <MediaPlayer/MediaPlayer.h>


#import "MusicModel.h"


#define W [UIScreen mainScreen].bounds.size.width
#define H [UIScreen mainScreen].bounds.size.height

static NSInteger currentIndex;


@interface ViewController ()<AVAudioPlayerDelegate>
@property (nonatomic, strong) LyricsView *lyricsView;//歌词视图，里面是两个lab
@property (nonatomic,weak) UIScrollView * backScrollView;//歌词滑动的ScrollView

@property (nonatomic, strong) MusicModel *playingMusic;

@property (nonatomic, strong) NSTimer *timer;

@end

@implementation ViewController
{
    AVAudioPlayer * _player;
    BOOL _isPlaying;
    //歌词
    NSString * _lyrics;
    CGSize size;
    int time;
    //每句的时间数组
    NSMutableArray * _timeArray;
    //换行时间数组
    NSMutableArray * _startTimeArray;
    //纯歌词
    NSMutableString * _lyricsStr;
    //纯歌词数组
    NSMutableArray * _lyricsSArray;
    //每行歌词单词个数的数组
    NSMutableArray * _wordNumArray;
    //最大行的歌词的个数
    int _maxNum;
    CGSize lineSize;
}




__strong static ViewController *singleViewController = nil;
+(ViewController *)sharedInstance
{
    static dispatch_once_t pred = 0;
    dispatch_once(&pred, ^{
        singleViewController = [[super allocWithZone:NULL] init];
    });
    NSLog(@"singleViewController = %@",singleViewController);
    return singleViewController;
}

+(id)allocWithZone:(struct _NSZone *)zone
{
    return [self sharedInstance];
}

-(id)copyWithZone:(NSZone *)zone
{
    return self;
}


- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor whiteColor];
    UIImageView * imageView = [[UIImageView alloc]init];
    imageView.frame = self.view.frame;
    [imageView setImage:[UIImage imageNamed:@"bj_sing"]];
    [self.view addSubview:imageView];
//    return;
    //拿到歌词
//    NSString *path = [[NSBundle mainBundle] pathForResource:@"那时的光 - 谭维维.Krc" ofType:nil];
//    NSString *urlString = [[NSBundle mainBundle] pathForResource:@"那时的光 - 谭维维.mp3" ofType:@""];
    
//    NSString *path = [[NSBundle mainBundle] pathForResource:@"女儿国 - 张靓颖、李荣浩.krc" ofType:nil];
//    NSString *urlString = [[NSBundle mainBundle] pathForResource:@"女儿国 - 张靓颖、李荣浩.mp3" ofType:@""];

    

    //创建ScrollView及其视图
    [self createBackScrollView];
    
    //创建点击开始按钮
    [self createPlayBtn];
    
    [self reloadSource];
    
//    [self reloadSource];

}
-(void)showInVC:(UIViewController *)vc withIndex:(NSInteger)index
{
    [vc.navigationController pushViewController:self animated:YES];
    currentIndex = index;
    [self playIndex:index];
}

-(void)playIndex:(NSInteger)index
{
    MusicModel *model = self.songArr[index];
    if (_model) {
        if (![model.name isEqualToString:_model.name]) {
            _model = model;
            UIButton *playBtn = [self.view viewWithTag:100];
            
            [self playBtnClick:playBtn];
            [self reloadSource];
        }
    }
    else
    {
        _model = model;
    }
}

-(void)reloadSource
{
    self.navigationItem.title = _model.name;
    NSString *path = _model.lyricPath;
    NSString *urlString = _model.songPath;
    
    urlString = [urlString stringByAddingPercentEncodingWithAllowedCharacters:[NSCharacterSet URLQueryAllowedCharacterSet]];
    NSURL *musicURL = [NSURL URLWithString:urlString];
    
    //播放歌
    //    NSURL * url = [[NSBundle mainBundle]URLForResource:@"G.E.M.邓紫棋 - 后会无期" withExtension:@"mp3"];
    //    NSURL * url = [[NSBundle mainBundle]URLForResource:@"vocal" withExtension:@"mp3"];
    //    _player = [[AVAudioPlayer alloc]initWithContentsOfURL:url error:nil];
    
    NSError *error;
    _player = [[AVAudioPlayer alloc] initWithContentsOfURL:musicURL error:&error];
    
    _player.delegate = self;
    [_player prepareToPlay];
    _isPlaying = YES;
    
    
    KRC * krc = [KRC new];
    _lyrics = [krc Decode:path];
    _playingMusic = [[MusicModel alloc] init];
    _playingMusic.name = _model.name;
    _playingMusic.icon = @"IMG_2925.JPG";
    _playingMusic.fileName = _model.fileName;
    _playingMusic.lyricName = _model.lyricName;//
    //    _playingMusic.lrcname = @"女儿国.Krc";//
    
    _playingMusic.singerIcon = @"IMG_2925.JPG";
    _playingMusic.singer = _model.singer;
    
    //拿到歌词的所有数据
    [self getLyricsInfo];
    
    
    
    //把字符串传给两个lab
    self.lyricsView.text = _lyricsStr;
    //得到歌词的行数
    self.lyricsView.textLable.numberOfLines =[LyricsUtil getLyricLineNumWithLyric:_lyrics];
    self.lyricsView.maskLable.numberOfLines =[LyricsUtil getLyricLineNumWithLyric:_lyrics];
    
    //设置两个lab的行间距
    NSMutableAttributedString *attributedString = [[NSMutableAttributedString alloc]initWithString:self.lyricsView.textLable.text];
    NSMutableParagraphStyle *paragraphStyle = [[NSMutableParagraphStyle alloc]init];
    [paragraphStyle setLineSpacing:10];
    [attributedString addAttribute:NSParagraphStyleAttributeName value:paragraphStyle range:NSMakeRange(0, [self.lyricsView.textLable.text length])];
    self.lyricsView.textLable.attributedText = attributedString;
    
    NSMutableAttributedString *attributedString1 = [[NSMutableAttributedString alloc]initWithString:self.lyricsView.maskLable.text];
    NSMutableParagraphStyle *paragraphStyle1 = [[NSMutableParagraphStyle alloc]init];
    [paragraphStyle1 setLineSpacing:10];
    [attributedString1 addAttribute:NSParagraphStyleAttributeName value:paragraphStyle1 range:NSMakeRange(0, [self.lyricsView.maskLable.text length])];
    self.lyricsView.maskLable.attributedText = attributedString1;
    self.lyricsView.textAlignment = NSTextAlignmentCenter;
    
    //正常步骤，知道歌词，知道lab字体大小算出歌词的
    size = [_lyricsStr sizeWithFont:self.lyricsView.textLable.font
                  constrainedToSize:(CGSize){self.lyricsView.textLable.frame.size.width, NSIntegerMax}
                      lineBreakMode:self.lyricsView.textLable.lineBreakMode];
    lineSize = [_lyricsSArray[0] sizeWithFont:self.lyricsView.textLable.font constrainedToSize:CGSizeMake(self.lyricsView.textLable.frame.size.width, MAXFLOAT) lineBreakMode:NSLineBreakByCharWrapping];
    
    size.height =(lineSize.height-9) * _lyricsSArray.count;
    
    self.backScrollView.contentSize = (CGSize){size.width, size.height+300};
    self.lyricsView.frame = CGRectMake(10, 10, W-20, size.height);
    
    [self.lyricsView.maskLable setFrame:CGRectMake(W/2-size.width/2, 0, size.width, size.height)];
    [self.lyricsView.textLable setFrame:CGRectMake(W/2-size.width/2, 0, size.width, size.height)];
    
    [UIView animateWithDuration:0.2 animations:^{
        self.backScrollView.contentOffset = CGPointMake(0, 0);
    } completion:^(BOOL finished) {
        UIButton *playBtn = [self.view viewWithTag:100];
        [self playBtnClick:playBtn];
    }];
    
    
    
}


#pragma mark ----锁屏时候的设置，效果需要在真机上才可以看到
- (void)updateLockedScreenMusic
{
    // 播放信息中心
    MPNowPlayingInfoCenter *center = [MPNowPlayingInfoCenter defaultCenter];
    
    // 初始化播放信息
    NSMutableDictionary *info = [NSMutableDictionary dictionary];
    // 专辑名称
    info[MPMediaItemPropertyAlbumTitle] = self.playingMusic.name;
    // 歌手
    info[MPMediaItemPropertyArtist] = self.playingMusic.singer;
    // 歌曲名称
    info[MPMediaItemPropertyTitle] = self.playingMusic.name;
    // 设置图片
    info[MPMediaItemPropertyArtwork] = [[MPMediaItemArtwork alloc] initWithImage:[UIImage imageNamed:self.playingMusic.icon]];
    // 设置持续时间（歌曲的总时间）
    info[MPMediaItemPropertyPlaybackDuration] = @(_player.duration);
    // 设置当前播放进度
    info[MPNowPlayingInfoPropertyElapsedPlaybackTime] = @(_player.currentTime);
    
    // 切换播放信息
    center.nowPlayingInfo = info;
    
    // 远程控制事件 Remote Control Event
    // 加速计事件 Motion Event
    // 触摸事件 Touch Event
    
    // 开始监听远程控制事件
    // 成为第一响应者（必备条件）
    [self becomeFirstResponder];
    // 开始监控
    [[UIApplication sharedApplication] beginReceivingRemoteControlEvents];
}

- (void)remoteControlReceivedWithEvent:(UIEvent *)event
{
    NSLog(@"远程控制");
    //    event.type; // 事件类型
    //    event.subtype; // 事件的子类型
    //    UIEventSubtypeRemoteControlPlay                 = 100,
    //    UIEventSubtypeRemoteControlPause                = 101,
    //    UIEventSubtypeRemoteControlStop                 = 102,
    //    UIEventSubtypeRemoteControlTogglePlayPause      = 103,
    //    UIEventSubtypeRemoteControlNextTrack            = 104,
    //    UIEventSubtypeRemoteControlPreviousTrack        = 105,
    //    UIEventSubtypeRemoteControlBeginSeekingBackward = 106,
    //    UIEventSubtypeRemoteControlEndSeekingBackward   = 107,
    //    UIEventSubtypeRemoteControlBeginSeekingForward  = 108,
    //    UIEventSubtypeRemoteControlEndSeekingForward    = 109,
    switch (event.subtype) {
        case UIEventSubtypeRemoteControlPlay:
        case UIEventSubtypeRemoteControlPause:
            NSLog(@"播放与暂停");
//            [self playOrPause:nil];
            break;
            
        case UIEventSubtypeRemoteControlNextTrack:
            NSLog(@"下一首");
//            [self next:nil];
            break;
            
        case UIEventSubtypeRemoteControlPreviousTrack:
            NSLog(@"上一首");
//            [self previous:nil];
            
        default:
            break;
    }
}

-(void)getLyricsInfo
{
    //得到没句的时间
    _timeArray = [LyricsUtil timeArrayWithLineLyric:_lyrics];
    //得到换行时间
    _startTimeArray = [LyricsUtil startTimeArrayWithLineLyric:_lyrics];
    //得到纯歌词
    _lyricsStr = [LyricsUtil getLyricStringWithLyric:_lyrics];
    //得到纯歌词数组
    _lyricsSArray = [LyricsUtil getLyricSArrayWithLyric:_lyrics];
    //每行歌词单词个数的数组
    _wordNumArray = [LyricsUtil getLineLyricWordNmuWithLyric:_lyrics];
    _maxNum = [LyricsUtil getMaxLineNumWithArray:_wordNumArray];
}

-(void)createBackScrollView
{
    UIScrollView * backScrollView = [[UIScrollView alloc]init];
    self.backScrollView = backScrollView;
    [self.view addSubview:backScrollView];
    backScrollView.frame = CGRectMake(0, 80, W, H-160);

    time = 0;
    LyricsView * lyricsView = [[LyricsView alloc] initWithFrame:CGRectMake(10, 100, 355, 60)];
    lyricsView.center = CGPointMake(CGRectGetWidth([UIScreen mainScreen].bounds) / 2, 200);
    lyricsView.backgroundColor = [UIColor clearColor];
    //传过去两个lab的字体大小
    lyricsView.font = [UIFont systemFontOfSize:16];

    

    [self.backScrollView addSubview:lyricsView];
    self.lyricsView = lyricsView;

    
}

-(void)createPlayBtn
{
    UIButton * playBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    playBtn.layer.masksToBounds = YES;
    playBtn.layer.cornerRadius = 30;
    [self.view addSubview:playBtn];
    playBtn.tag = 100;
    playBtn.frame = CGRectMake([UIScreen mainScreen].bounds.size.width/2-30,H-60, 60, 60);
    [playBtn addTarget:self action:@selector(playBtnClick:) forControlEvents:UIControlEventTouchUpInside];
    playBtn.selected = NO;
    [playBtn setTitle:@"播放" forState:UIControlStateNormal];
    [playBtn setTitle:@"停止" forState:UIControlStateSelected];
    playBtn.backgroundColor = [UIColor greenColor];
}

//播放结束的时候会被调用
-(void)audioPlayerDidFinishPlaying:(AVAudioPlayer *)player successfully:(BOOL)flag
{
    NSLog(@"播放结束");
    currentIndex++;
    if (currentIndex < self.songArr.count) {
        [self playIndex:currentIndex];
    }
    else
    {
        currentIndex--;
    }
}
//协议中的方法，当播放被打断时被调用。当播放音乐时电话响了，短信响了都会打断音乐播放，我们通常需要暂停播放
-(void)audioPlayerBeginInterruption:(AVAudioPlayer *)player
{
    [_player stop];//暂停而不是停止
}
//协议中的方法，当结束打断（比如电话接完了）的时候会被调用
-(void)audioPlayerEndInterruption:(AVAudioPlayer *)player
{
    if (_isPlaying) {
        [_player play];//继续播放
    }

}

-(void)playBtnClick:(UIButton *)btn
{
    if (btn.selected == NO) {
        [_player play];
        [self updateLockedScreenMusic];
        //设置监控 每秒刷新一次时间
        if (!_timer) {
            _timer = [NSTimer scheduledTimerWithTimeInterval:0.01f target:self selector:@selector(changeTime) userInfo:nil repeats:YES];
        }
        if (self.lyricsView.maskLayer == nil) {
            [self.lyricsView setupDefault];
        }

        btn.selected = YES;
    }else{

        btn.selected = NO;
        [_player stop];
        [self.lyricsView stopAnimation];
    }

}

//判断换行
-(void)changeTime
{

//    NSLog(@"_player.currentTime = %f",_player.currentTime);
//    NSLog(@"_player.duration = %f",_player.duration);
    if (_player.currentTime >= _player.duration) {
        NSLog(@"播放结束了");
    }

    for (int i=0; i<_startTimeArray.count; i++) {
        int startTime = [_startTimeArray[i] intValue];
        float currentTime = startTime*1.0/1000;
        if (currentTime - _player.currentTime>=0 && currentTime - _player.currentTime<=0.2) {
            [self changeLineWithNmu:i];
        }

    }

}
//改变变色mask位置和动画传值
-(void)changeLineWithNmu:(int)num
{

    self.lyricsView.maskLayer.position = CGPointMake(0,(lineSize.height-9)*(num));
    self.lyricsView.maskLayer.bounds = CGRectMake(0, 0, 0, lineSize.height-9);

    if (num >= 8) {
        [self.backScrollView setContentOffset:CGPointMake(0,(num-7)*(lineSize.height-9)+5) animated:YES];
    }

    //timeArray每行歌词的每个单词的开始时间，必须以0开头，总时间结尾
    NSArray *timeArray = _timeArray[num];
    NSMutableArray * timeSumArray = [[NSMutableArray alloc]init];
    int wordNmu = [_wordNumArray[num]intValue];
    int start =(_maxNum-wordNmu)/2;
    int end = start+wordNmu;
    for (int y=0; y<=_maxNum; y++) {
        if (y<=start) {
            [timeSumArray addObject:@"0"];
        }else if(y>end){
            [timeSumArray addObject:timeArray[timeArray.count-1]];
        }
        else{
            [timeSumArray addObject:timeArray[y-start-1]];
        }

    }

    NSArray * timeEndArray = timeSumArray;
    //计算
    //locationArray每行歌词的每个单词在相应时间时对应的位置，假设为1总长，在歌词view里用比例乘宽度得到位置
    NSMutableArray * localArray =[[NSMutableArray alloc]init];
    for (int i=0; i<=_maxNum; i++) {
        float n = i*1.0/_maxNum;
        NSString * wordSNum = [NSString stringWithFormat:@"%lf",n];
        [localArray addObject:wordSNum];
    }
    NSArray *locationArray = localArray;
    [self.lyricsView startLyricsAnimationWithTimeArray:timeEndArray andLocationArray:locationArray];

}

-(void)dealloc
{
    NSLog(@"销毁音乐播放控制器");
}

@end























